//
//  WHSettingController.m
//  SquirrelSeller
//
//  Created by 邓伟浩 on 2019/1/6.
//  Copyright © 2019 邓伟浩. All rights reserved.
//

#import "WHSettingController.h"
#import "WHSettingCell.h"

@interface WHSettingController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *dataTableView;

@end

@implementation WHSettingController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"设置";
    [self setUpTableView];
}

- (void)setUpTableView {
    UITableView *dataTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    dataTableView.delegate = self;
    dataTableView.dataSource = self;
    [self.view addSubview:dataTableView];
    
    [dataTableView registerClass:[WHSettingCell class] forCellReuseIdentifier:NSStringFromClass([WHSettingCell class])];
    
    dataTableView.tableFooterView = [UIView new];
    dataTableView.backgroundColor = CViewBgMainColor;
    dataTableView.separatorColor = CLineColor;
    dataTableView.separatorInset = UIEdgeInsetsMake(0, KNormalSpace, 0, -KNormalSpace);
    
    [dataTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self.view);
        make.top.equalTo(@kTopHeight);
    }];
    _dataTableView = dataTableView;
}

#pragma mark - —————— UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WHSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([WHSettingCell class]) forIndexPath:indexPath];
    
    kWeakSelf(self);
    if (indexPath.row == 0) {
        cell.tipsLabel.text = @"入库提示音";
        BOOL isInVoice = [kUserDefaults boolForKey:InVoice];
        cell.isOpen = isInVoice;
        cell.switchClicked = ^{
            [kUserDefaults setBool:!isInVoice forKey:InVoice];
            [kUserDefaults synchronize];
            [weakself.dataTableView reloadData];
        };
    } else {
        cell.tipsLabel.text = @"出库提示音";
        BOOL isOutVoice = [kUserDefaults boolForKey:OutVoice];
        cell.isOpen = isOutVoice;
        cell.switchClicked = ^{
            [kUserDefaults setBool:!isOutVoice forKey:OutVoice];
            [kUserDefaults synchronize];
            [weakself.dataTableView reloadData];
        };
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kRealwidth(55);
}

@end
